const SERVICE = require("../../lib/js/service.js"); 
 
  Page({
    
    onLoad(){
      
    },
    onShow(){
      this.getWithdrawInfo();
    },

    /*分享*/
    onShareAppMessage(){} ,
    

    /**
     * @exlian getWithdrawInfo 获取提现信息
     * @param thirdCookie 用户标识
     * 
     * */ 
    getWithdrawInfo() {
      wx.showNavigationBarLoading();
      let _this = this;
      SERVICE.POST(SERVICE.URL.getWithdrawInfo, {
        thirdCookie: wx.getStorageSync("thirdCookie")
      }, (status, res) => {
        wx.hideNavigationBarLoading();
        console.log(res);
        if (res.data.status === "success") {
          _this.setData({
            info: res.data
          });
        } else {
          wx.showToast({
            title: "获取信息失败",
            icon: "none",
            mask: true
          });
        }
      });
    },

    /**
     * @explain formSubmit 提现
     * @param thirdCookie 用户信息
     * @param money   提现金额
     * @param   card_id 银行卡ID
     * 
     * */ 
    formSubmit(e){
      let td = this.data, form_obj = e.detail.value, money = e.detail.target.dataset.all, id = e.detail.target.dataset.id;
      if (td.info.bankInfoList.length == 0) {
        wx.showToast({
          title: "请添加银行卡",
          icon: "none",
          mask: true
        });
      } else if(!form_obj.sum){
        wx.showToast({
          title: "请输入提现金额",
          icon: "none",
          mask: true
        });
      } else if (parseFloat(form_obj.sum) > parseFloat(money)){
        wx.showToast({
          title: "提现超过最大金额",
          icon: "none",
          mask: true
        });
      }else {
       this.withdrawDeposit({
         money: form_obj.sum,
         card_id: id
       });
      }
    },

    all(e){
      let td = this.data, sum = parseFloat(e.currentTarget.dataset.all), id = e.currentTarget.dataset.id;
      if (td.info.bankInfoList.length == 0) {
        wx.showToast({
          title: "请添加银行卡",
          icon: "none",
          mask: true
        });
      } else if (sum > 0){
        this.setData({
          sum
        });
        // this.withdrawDeposit({
        //   money: sum,
        //   card_id: id
        // });
      }else{
        wx.showToast({
          title: "余额不足",
          icon: "none",
          mask: true
        });
      }
    },
    
    /**
     * @explain withdrawDeposit 提现
     * @param thirdCookie 用户信息
     * @param money   提现金额
     * @param   card_id 银行卡ID
     * 
     * */ 
    withdrawDeposit(params){
      params.thirdCookie = wx.getStorageSync("thirdCookie");
      wx.showLoading({
        title: "提现中...",
        mask: true
      });
      let _this = this;
      SERVICE.POST(SERVICE.URL.withdrawDeposit,params, (status, res) => {
        wx.hideLoading();
        if (res.data.status === "success") {
          wx.showToast({
            title: res.data.message,
            icon: "none",
            mask: true,
            success: function () {
              _this.getWithdrawInfo();
            }
          });
        } else {
          wx.showToast({
            title: res.data.message,
            icon: "none",
            mask: true
          });
        }
      });
    }
    
  })